/*
* @author suhong
* @date 2022/5/18 12:29
* @file userInfo.js
* @说明  获取用户信息接口的处理函数
*/
//导入数据库的模块
const exec = require('../../db/mysql_connect')
const path = require("path");
const fs = require("fs");
const images = require('images');
const formidable = require("formidable");
//获取用户的信息
exports.userInfo = (req, res) => {
    // res.send(req.query)
    //获取用户的id
    let userId = req.query.id

    //查询语句
    let sqlStr = `select register_time,about_us,user_name,user_icon from blog_user where user_id=${userId}`
    exec(sqlStr).then(result => {
        // console.log(result)
        res.send({
            status: 0,
            result:result[0]
        })
    }).catch(reason => {
        res.status(500).send({
            message: reason
        })
    })
}
//获取用户的头像
exports.getUserIcon = (req, res) => {
    let userID = req.query.id
    let sqlStr = `select user_icon from blog_user where user_id=${userID}`
    console.log(req.query)
    exec(sqlStr).then(result => {
        res.send({
            status: 0,
            user_icon: result[0].user_icon
        })
    }).catch(reason => {
        res.send(reason)
    })
    // exec()
}
//上传头像
exports.userIcon = (req, res) => {
    // console.log(req.files);
    // console.log(path.parse(req.files[0].originalname).ext)
    let oldname = req.files[0].path //获取path: 'public\\upload\\0f625978d5d1a783b12e149718f8b634',
    //这个是新文件名字
    let newname = (req.files[0].path + path.parse(req.files[0].originalname).ext)//.jpg
    // console.log(newname)
    // console.log(newname)
    let userId = req.query.id

    console.log(newname)
    return new Promise((resolve, reject) => {
        try {
            fs.renameSync(oldname, newname)
            resolve()
        } catch {
            reject()
        }
    }).then(() => {
        newname = "http://localhost:5000/" + newname.replace("public\\", "").replace('\\', '/')
        exec(`update blog_user set user_icon='${newname}' where user_id=${userId}`).then(() => {
            res.send({status: 0, message: '头像以及上传'})
        })
    }).catch(() => res.send({err: 1, msg: '上传失败'}))
}
//修改用户信息
exports.updateUser=(req,res)=>{
    //用户的id
    let userId=req.body.id
    //用户的名字
    let userName=req.body.userName
    //用户的简介
    let userAbout=req.body.aboutUs
    //sqlStr语句
    let sqlStr=`update blog_user set user_name="${userName}",about_us="${userAbout}" where  user_id=${userId}`
    exec(sqlStr).then(()=>{
        res.send("success")
    }).catch(reason => {
        res.send(reason)
    })
}
//模糊查询用户名
exports.likeName=((req,res)=>{
    // res.send('success')
    //获取用户输入的信息
    let userName=req.query.userName
    let newUserName=''
    for(let i=0;i<userName.length;i++){
        newUserName+="%"+userName[i]+'%'
    }
    // res.send(newUserName)
    //sql语句
    let sqlStr=`select user_name,user_id from blog_user where user_name like '${newUserName}'`
    exec(sqlStr).then(value => {
        res.send({
            status:0,
            result:value
        })
    }).catch(reason => {
        res.send(reason)
    })
})




    
    
